export default function createStore(reducers) {
	let store = {
		state: {},
		callbacks: [],
		init() {
			Object.keys(reducers).forEach(reducer => {
				let callback = reducers[reducer];
				this.state[reducer] = callback({type: Math.trunc(Math.random() * 100000) + '@init@000', data: null});
			})
		},
		dispatch(action) {
			if (typeof action === 'function') { // 异步action
				action(this.dispatch, JSON.parse(JSON.stringify(this.state)));
			} else {
				Object.keys(reducers).forEach(reducer => {
					let callback = reducers[reducer];
					let preState = store.state[reducer];
					store.state[reducer] = callback(action, preState);
				})
				store.publish();
			}
		},
		getState() {
			return this.state;
		},
		subscribe(callback) {
			this.callbacks.push(callback);
		},
		publish() {
			if (this.callbacks.length) {
				this.callbacks.forEach(callback => {
					callback(this.state);
				})
			}
		},
	}
	store.init();
	return store;
}
